package com.usian.search.listener;

import com.usian.search.service.ApArticleSearchService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

import static com.usian.common.constants.message.Article2EsConstants.ARTICLE_2_ES_TOPIC;

/**
 * 文章审核通过 消息监听器
 */
@Component
public class ArticleListener {

    @Autowired
    private ApArticleSearchService apArticleSearchService;

    @KafkaListener(topics = ARTICLE_2_ES_TOPIC)
    public void receiveMessage(ConsumerRecord<?,?> record){
        Optional<? extends ConsumerRecord<?, ?>> record1 = Optional.ofNullable(record);
        if (record1.isPresent()){
            //消息内容
            Object value = record.value();
            //转为long类型
            Long articleId = Long.valueOf(value.toString());
            //调用方法
            apArticleSearchService.ImportOneArticle2Es(articleId);
        }
    }
}
